package cn.edu.nhic.order;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import cn.edu.nhic.order.bean.ShopBean;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private MsgHandle msgHandle;
    private List<ShopBean> shopBeanList;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        msgHandle=new MsgHandle();
        this.initView();
        this.initData();
    }
    public void initView(){
        imageView=this.findViewById(R.id.imageView);
    }
    public void initData(){
        //知识点1:网络访问 步骤
        //1.创建OkHttpClient对象
        OkHttpClient client=new OkHttpClient();
        //2.构建Request对象
        String url="http://192.168.3.195:8080/order/shop_list_data.json";
//        String url="https://www.baidu.com";
//        String url="https://www.qypt.edu.cn/";
        Request request=new Request.Builder()
                .url(url)
                .get()
                .build();
        //3.创建Call对象  呼叫
        Call call=client.newCall(request);
        //把异步网络访问的请求加入队列
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.i("MainActivity isSuccessful========",response.isSuccessful()+"");
                String res=response.body().string();
                Log.i("MainActivity=========",res);
                System.out.println("MainActivity========="+res);
                Message msg=new Message();
                msg.what=1;
                msg.obj=res;
                //发送消息
                msgHandle.sendMessage(msg);
            }
        });
    }
    class MsgHandle extends Handler{
        //分发 分配信息
        @Override
        public void dispatchMessage(@NonNull Message msg) {
            super.dispatchMessage(msg);
            //
            String shopJson=(String)msg.obj;
//            Log.i("MainActivity Handler=========",shopJson);
//            System.out.println("MainActivity Handler========="+shopJson);
            //知识点2：将JSON转为对应的JavaBean 用到Gson
            //步骤1：创建Gson对象
            Gson gson=new Gson();
            //步骤2：
            Type type=new TypeToken<List<ShopBean>>(){}.getType();
            //步骤3：
            shopBeanList=gson.fromJson(shopJson,type);
            for(int i=0;i<shopBeanList.size();i++){
                Log.i("shopList>>>>>>",shopBeanList.get(i).getShopPic());
            }
            //知识点3：把网络图片加载到UI组件上Glide
            //Glide.with(上下文).load(图片资源).into(anroidUI组件)
            Glide.with(getApplicationContext())
//                    .load(shopBeanList.get(0).getShopPic())
                    .load("http://192.168.3.195:8080/order/img/shop/shop5.png")
                    .into(imageView);
        }
    }
}